package com.yd.app.service.pc.orderService;

import com.yd.app.mapper.OrderMapper;
import com.yd.app.service.common.BaseService;
import com.yd.domain.business.Order;
import com.yd.domain.business.OrderCoupon;
import com.yd.domain.business.OrderGoods;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.OrderParam;
import com.yd.domain.sys.DicEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单-商品
 * Created by sunxianhui on 2018/3/7.
 */
@Service
public class OrderService extends BaseService{
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 添加订单
     * @param order
     */
    public int addOrder(Order order) {
        return orderMapper.addOrder(order);
    }

    /**
     * 添加订单——商品
     * @param orderGoods
     */
    public void addOrderGoods(OrderGoods orderGoods) {
        orderMapper.addOrderGoods(orderGoods);
    }

    /**
     * 添加订单——使用优惠券
     * @param orderCoupon
     */
    public void addOrderCoupon(OrderCoupon orderCoupon) {
        orderMapper.addOrderCoupon(orderCoupon);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteOrderByid(String id) {
        orderMapper.deleteOrderById(id);
    }

    /**
     * 修改
     * @param order
     */
    public void updateOrder(Order order) {
        orderMapper.updateOrder(order);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Order selectOrderById(String id) {
        Order order = orderMapper.selectOrderById(id);
        if(order==null){
            return null;
        }else {
            if(order.getPayStatus()!=null&&order.getPayStatus().equals(DicEnum.PAY_STATUS.UN_PAY)){
                order.setPayStatusName(DicEnum.PAY_STATUS.UN_PAYNAME);
            }else{
                order.setPayStatusName(DicEnum.PAY_STATUS.PAYNAME);
            }
            if(order.getStatus()!=null&&order.getStatus().equals(DicEnum.ORDER_STATUS.UN_DEAL)){
                order.setStatusName(DicEnum.ORDER_STATUS.UN_DEALNAME);
            }else{
                order.setStatusName(DicEnum.ORDER_STATUS.DEALNAME);
            }
            order.setGoods(orderMapper.selectGoodsByOrderId(id));
            if (order.getIfCoupon()) {
                order.setCoupon(orderMapper.getCoupon(order.getOrderId()));
            }

            List<String>typeOneList=new ArrayList<>();//一级Ids
            List<String>typeIdList=new ArrayList<>();//二级ids
            for(OrderGoods og:order.getGoods()) {
                if (!typeOneList.contains(og.getTypeOne())) {
                    typeOneList.add(og.getTypeOne());
                }
                if (!typeIdList.contains(og.getType())) {
                    typeIdList.add(og.getType());
                }
            }
            order.setgTypeOneIdlist(typeOneList);
            order.setgTypeIdlist(typeIdList);

        }
        return order;
    }

    /**
     * 根据条件分页查询
     * @param orderParam
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public PageModel getOrderList(OrderParam orderParam, int pageIndex, int pageSize) {
        PageModel pm = new PageModel();
        int startRow = getStartRow(pageIndex, pageSize);
        List<Order> orderList = orderMapper.getOrderList(orderParam, startRow, pageSize);
        for(int i=0;i<orderList.size();i++){
            orderList.get(i).setGoods(orderMapper.selectGoodsByOrderId(orderList.get(i).getOrderId()));
        }
        int count = 0;
        if (pageIndex == 1) {
            //查询总条数
            count = orderMapper.getOrderCount(orderParam);
            pm.setCount(count);
        }
        pm.setList(orderList);
        return pm;
    }

    /**
     * 审核订单状态
     * @param orderId
     */
    public void reviewOrder(String orderId,String status) {
        orderMapper.reviewOrder(orderId,status);
    }

    /**
     * 付款状态
     * @param orderId
     * @param payStatus
     */
    public void payStatus(String orderId,String payStatus) {
        orderMapper.payStatus(orderId,payStatus);
    }

    /**
     * 物流快递单号
     * @param orderId
     * @param expressCode
     */
    public void expressCode(String orderId,String expressCode) {
        orderMapper.expressCode(orderId,expressCode);
    }

    /**
     * 物流快递单号
     * @param order
     */
    public void addExpressCode(Order order) {
        orderMapper.updateExpressCode(order);
    }

    /**
     * 修改配货状态
     * @param goods
     */
    public void updatePhStatus(OrderGoods goods) {
        orderMapper.updatePhStatus(goods);
    }
}
